<template>
  <el-dialog
    title="提示"
    :visible="showDialog"
    width="30%"
    @close="closeDialog"
    @open="openDialog"
  >
    <template #default>
      <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <!-- label:文案  value:选中哪个哪个的value会被自动绑定到select v-model后面的属性上 一般和后端交互 提交给后端 -->
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
        </el-form-item>
      </el-form>
    </template>
    <!-- 具名插槽 按钮操作区域 -->
    <template #footer>
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employee'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/department'
export default {
  // props传入的数据可以直接修改吗？ readonly 单项数据流 通过自定义事件修改父组件中的数据 emit
  // 父子通信的方法
  props: {
    showDialog: {
      type: Boolean
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      managerList: []
    }
  },
  methods: {
    closeDialog() {
      console.log('弹框要关闭了')
      // 通过子传父的方式 把showDialog改成false
      this.$refs.deptForm.resetFields()
      this.$emit('close-dialog')
    },
    async openDialog() {
      const res = await getEmployeeSimple()
      this.managerList = res
    },
    confirm() {
      this.$refs.deptForm.validate(async valid => {
        if (valid) {
          if (this.form.id) {
            await updateDepartments(this.form)
          } else {
            await addDepartments({...this.form, pid: this.curId})
          }
          // 刷新列表数据
          this.$emit('reset-data')
          // 关闭弹窗
          this.$emit('close-dialog')
          // 提示用户
          this.$message.success(`${this.form.id ? '更新成功' : '新增成功'}`)
          // 数据清空重置
          this.$refs.deptForm.resetFields()
          this.form.id = ''
        }
      })
    },
    async getDetail(id) {
      const res = await getDepartDetail(id)
      this.form = res
    },
    cancel() {
      this.$emit('close-dialog')
      this.$refs.deptForm.resetFields()
      this.form.id = ''
    }
  }
}
</script>
